<?php 

class infoController extends appController
{
    public $wxid = 0;//数据库Id
    
    public $m;//自己的model
    public $info=array();//当前用户的微信信息
    public  function __construct(){
        parent::__construct();
        //基础信息构建
        $this->m = Model('suipai');
       
    
        //当前用户的微信id
        $this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
    
        if($this->wxid>0)
        {
            //当前用户的微信信息
            $this->info = $this->m->get_record($this->wxid);
            $this->assign('info',$this->info);
        }
        
    
    }
	
	public function clear()
	{
		
		unset($_SESSION['wxid']);
		unset($_SESSION);
	}
	
	public function canIn()
	{
	     
	    if(!$this->info)
	    {
	       
	        echo "非法操作";
	        exit;
	    }
	}
	
	
	
	public function ajaxCard()
	{
	   
	       
	    $cardList[]['cardId'] = "plHTpwtCzwqw_-zhGTGCGBP-cdhY";
	     
	    $jsdk_card = $this->m->build_jsdk_card($cardList);

	    $res['error'] =0;
	    $res['data'] =$jsdk_card;
	    
	    echo json_encode($res);
	   
	}
	

	
	public function record()
	{
	    //微信分享参数构建
	    $jsdk = $this->m ->get_jsdk();
	    
	    $this->assign("jsdk", $jsdk);
	  
		$this->check_member2();
		
		$this->assign_global($config);
	   
		$this->display("record.html");
	}
	
	
	
	public function record2()
	{
	   $_SESSION['wxid'] = 24759;
	     
	   //微信分享参数构建
	   $jsdk = $this->m ->get_jsdk();
	   
	   $this->assign("jsdk", $jsdk);
	   
	    $this->check_member2();
	
	    $this->assign_global($config);
	
	    $this->display("record.html");
	}
	
	public function saveUser(){
	    
	    $this->canIn();
	    
	    $res = array("error"=>0,"content"=>"保存信息成功");
	    
	    $back = $this->m->is_saved_user($_SESSION['wxid']);
	    
	    if($back['status']>0){
	        $res['error'] = 2;
	        $res['content'] = "你已经领取过了。";
	        die(json_encode($res));
	    }
	    
	    if($back)
	    {
	        $this->ajaxCard();
	    }
	    else
	    {
    	    $user['phone'] =isset($_REQUEST['phone'])?$_REQUEST['phone']:"";
    	    if(preg_match("/1[3458]{1}\d{9}$/",$user['phone'])){
    	
    	    }else{
    	        $res['error'] = 1;
    	        $res['content'] = "请填写正确的11位手机号码。";
    	        die(json_encode($res));
    	    }
    	    $user['username'] = isset($_REQUEST['username'])?$_REQUEST['username']:"";
    	    
    	    $user['wxid'] = $_SESSION['wxid'];
    	    $user['addtime'] = time();
    	    if(!$back)
    	    {
    	        
        	    $id = $this->m->insert($user,"info_record");
    	    }
    	    $this->ajaxCard();
	    }
	    
	}
	
	
	public function getCard()
	{
	    $id = $this->wxid;
        
	    if($id == 0)
	    {
	        die("error".$id);
	    }
	    
	    $this->m->saveInfo($id);

	    echo "success".$id;
	    
	}
	

}

?>